package com.shujia.day15.udp.image;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Client {
    public static void main(String[] args) throws IOException {
        /*
            TODO 通过UDP协议实现数据发送
         */
        DatagramSocket datagramSocket = new DatagramSocket();
        System.out.println("Socket启动了");

        // TODO 1. 通过IO流方式读取图片数据
        byte[] bytes = new byte[1024];

        FileInputStream fileInputStream = new FileInputStream("data/th.jpg");
        int readLen = 0;
        // TODO readLen 表示每次循环读取到的数据长度
        while ((readLen = fileInputStream.read(bytes)) > 0){
            //  TODO 根据读取的字节数组和读取长度构建 DatagramPacket 数据包
            DatagramPacket dataPacket = new DatagramPacket(bytes, readLen, InetAddress.getByName("localhost"), 8888);
            datagramSocket.send(dataPacket);
            System.out.println("数据发送了一部分...");
        }
        System.out.println("数据发送完成..");
        fileInputStream.close();
        datagramSocket.close();
    }
}
